<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <title>{$page_info.title}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>

<body>
<div style="padding: 8px!important;">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" id="{$table_id}_data_list" lay-filter="{$table_id}_data_list"></table>
        </div>
    </div>
</div>
<script type="text/html" id="{$table_id}_rightbar">
    <a class="layui-btn layui-btn-xs {{= (d.install === 2 || d.is_config === 2) ? 'layui-hide' : '' }}" lay-event="{$table_id}_edit">设置</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {{= (d.install === 2) ? 'layui-hide' : '' }}" lay-event="{$table_id}_del">卸载</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs {{= (d.install === 2) ? 'layui-hide' : '' }} {{= (d.update === 2) ? 'layui-hide' : '' }}" lay-event="{$table_id}_update">更新</a>
    <a class="layui-btn layui-btn-xs {{= (d.install === 1) ? 'layui-hide' : '' }}" lay-event="{$table_id}_install">安装</a>
</script>
<script type="text/html" id="{$table_id}_toptool">
    <a class="layui-btn layuiadmin-btn-useradmin" id="{$table_id}_addons_upload">上传插件</a>
</script>
<script type="text/html" id="{$table_id}_data_list-table-status">
    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-{{= (d.status === 1) ? 'normal' : (d.status === 2) ? 'danger' : 'warm'}}" onclick="set_status('{{= d.name}}','{{= d.status}}')">{{= (d.status === 1) ? '正常' : (d.status === 2) ? '禁止' : '未安装/异常'}}</button>
</script>
<script>
    layui.use(['table', 'treeTable','toast','popup'], function () {
        var $ = layui.jquery;
        var popup = layui.popup;
        var toast = layui.toast;
        var upload = layui.upload;
        var toast = layui.toast;
        var table_type = layui.table;

        // 创建渲染实例
        var insTb = table_type.render({
            elem: '#{$table_id}_data_list',
            toolbar: '#{$table_id}_toptool',
            height: 'full-150', // 最大高度减去其他容器已占有的高度差
            css: [ // 重设当前表格样式
                '.layui-table-tool-temp{padding-right: 145px;}'
            ].join(''),
            cellMinWidth: 80,
            totalRow: false, // 开启合计行
            page: true,
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field:'id', fixed: 'left', width:80, title: 'ID', sort: true},
                { field:'name', title: '插件标识'},
                { field:'title', title: '插件名称'},
                { field:'description', title: '描述'},
                { field:'status', title: '状态',templet: '#{$table_id}_data_list-table-status'},
                { field:'author', title: '作者'},
                { field:'version', title: '版本号'},
                { field:'create_at', title: '安装时间'},
                { field:'update_at', title: '更新时间'},
                { fixed: 'right', title: '操作', width: 200, minWidth: 125, toolbar: '#{$table_id}_rightbar' }
            ]],
            data:{$info|raw},
        });

        table_type.on('tool({$table_id}_data_list)', function (obj) {
            var event = obj.event;
            var data = obj.data;
            if (event === '{$table_id}_del') {
                layer.msg('确认卸载此插件吗?',{
                    time:5000,
                    shade:0.3,
                    btn:['卸载','取消'],
                    yes:function() {
                        $.post("{:url('admin/addons/uninstall')}",{id:data.name},function(data){
                            if(data.code === 200){
                                popup.success(data.msg, function () {
                                    PearAdmin.refresh();
                                });
                            }else{
                                toast.error({
                                    message: data.msg,
                                });
                            }
                        })
                    }
                });
            } else if (event === '{$table_id}_edit') {
                layer.open({
                    type:2,
                    title:"插件配置",
                    shade:0.3,
                    area:['95%','95%'],
                    content:"{:url($operate.edit)}"+"?id="+data.name,
                    end: function(){
                        PearAdmin.refresh();
                    }
                });
            } else if (event === '{$table_id}_install') {
                layer.msg('确认安装此插件吗?',{
                    time:5000,
                    shade:0.3,
                    btn:['安装','取消'],
                    yes:function() {
                        $.post("{:url('admin/addons/install')}",{id:data.name},function(data){
                            if(data.code === 200){
                                popup.success(data.msg, function () {
                                    PearAdmin.refresh();
                                });
                            }else{
                                toast.error({
                                    message: data.msg,
                                });
                            }
                        })
                    }
                });
            } else if (event === '{$table_id}_update') {
                layer.msg('确认更新此插件吗?',{
                    time:5000,
                    shade:0.3,
                    btn:['更新','取消'],
                    yes:function() {
                        $.post("{:url('admin/addons/addons_update')}",{id:data.name},function(data){
                            if(data.code === 200){
                                popup.success(data.msg, function () {
                                    PearAdmin.refresh();
                                });
                            }else{
                                toast.error({
                                    message: data.msg,
                                });
                            }
                        })
                    }
                });
            }
        });
        upload.render({
            elem: '#{$table_id}_addons_upload',
            field:'addons',
            accept:'file',
            exts:'zip',
            url: '{:url("admin/addons/upload_addons")}',
            done: function(res){
                if(res.code === 200){
                    popup.success(res.msg, function () {
                        PearAdmin.refresh();
                    });
                }else{
                    toast.error({
                        message: res.msg,
                    });
                }
            }
        });

        window.set_status = function(name,status){
            var msg = '确定启用插件吗？'
            if(status === '1'){
                status = 2;
                msg = '确定禁用插件吗？'
            }else if(status === '2'){
                status = 1;
            }else{
                toast.error({
                    message: '请先安装插件',
                });
                return false;
            }
            layer.msg(msg,{
                time:5000,
                shade:0.3,
                btn:['确定','取消'],
                yes:function() {
                    $.post("{:url('admin/addons/set_status')}",{name:name,state:status},function(data){
                        if(data.code === 200){
                            popup.success(data.msg, function () {
                                PearAdmin.refresh();
                            });
                        }else{
                            toast.error({
                                message: data.msg,
                            });
                        }
                    })
                }
            });
        }
    });

</script>
</body>
</html>